unit oSS;

interface

uses oPedActor, oVector, oMathematic, oSimulation, oBehaviorTypes, oCoordinates,
  Dialogs, sysutils;

procedure Update(paActor: TPedActor; paDeltaTime: TFloat); forward;

function SSSim(paActor: TPedActor): boolean; forward;

implementation

uses  oLogicalEntity;

procedure Update(paActor: TPedActor; paDeltaTime: TFloat);
var
  TotalVelocity: TVector;
  dest : TCoordinates;
  j : integer;
  f : text;
begin
    if (paActor.Pedestrian.BehaviorsList <> nil) and (paDeltaTime <> 0.0) then begin
        TotalVelocity := paActor.Pedestrian.BehaviorsList.Calculate(paActor);
       // if not aStopped then begin
           with paActor.Pedestrian do
            begin
              Velocity.AddSelf(TotalVelocity);
              Velocity.TruncateSelf(paActor.Pedestrian.MaxVelocity * paActor.Pedestrian.MaxAcceleration );
              Velocity.MultiplySelf(paDeltaTime);
              SSPosition.AddSelf(paActor.Pedestrian.Velocity);

              dest := TCoordinates.Create(paActor.Pedestrian.SSPosition.X,
                    paActor.Pedestrian.SSPosition.Y);
            // update on scene

              {assign(f, 'c:/test.txt');
              Append(f);
              writeln(f, IntToStr(paActor.ID) + ' pos: [' + FloatToStr(paActor.Pedestrian.SSPosition.X)
                + ',' + FloatToStr(paActor.Pedestrian.SSPosition.Y));
//                + '] dest : [' + FloatToStr(dest.X)
  //              + ',' + FloatToStr(dest.Y) + ']');
              close(f);
               }
              if not (paActor.MySim as TPedSimulation).World.getLevel(paActor.Pedestrian.Level).
                updatePedestrian(paActor.Pedestrian, dest) then
                  j := 0;
             //    ShowMessage('fail');
              dest.Free;


              {SSPosition.X := Position.X;
              SSPosition.Y := Position.Y; }
           { paActor.Pedestrian.Position.X := paActor.Pedestrian.SSPosition.X;
            paActor.Pedestrian.Position.Y :=  paActor.Pedestrian.SSPosition.Y;}
              if Velocity.Length <> 0 then LocalSpace.Setup(SSPosition, Velocity);
          end;
        TotalVelocity.Free;
    end;
end;

function SSSim(paActor: TPedActor): boolean;
var r : boolean;
begin


  r := (paActor.Target as TLogicalEntity).Shape.isWithinCircle(
    paActor.Pedestrian.Position, paActor.Pedestrian.Diameter/2);

  if not r then
    Update(paActor, TickDuration);

  if (paActor.Target.Id = 1) and (r) then
    r := (paActor.Target as TLogicalEntity).Shape.isWithinCircle(
      paActor.Pedestrian.Position, paActor.Pedestrian.Diameter/2);

//  IsInner((paActor.Target as TLogicalEntity).Shape.Rect, paActor.Pedestrian.Position);
//  paActor.TickSubscriber := r;
  result := r;
end;

end.

